/*
 * LocalRollUpMaximumZ.java
 *
 * Created on January 31, 2005, 2:38 PM
 */

package edu.temple.GUS.MMA.Functions;

import edu.temple.GUS.MMA.DataTypes.*;
import edu.temple.GUS.MMA.Exceptions.MMAFunctionInitException;
import edu.temple.GUS.MMA.util.StatisticalOperator;

/**
 *
 * @author  jeffleon
 */
public class LocalRollUpMaximumZ {
    
    /** Creates a new instance of LocalRollUpMaximumZ */
    public LocalRollUpMaximumZ() {}
    
    // Roll up a space cube along z-dimension
    public MMAGrid execute( MMASpaceCube sCube ) throws MMAFunctionInitException
    {
        if (sCube == null)
            throw new MMAFunctionInitException(this.getClass().toString() + " Space cube is not initialized.");

        // Initialze the dimensions of the spacecube and the noData value
        int x = sCube.getX();
        int y = sCube.getY();
        int z = sCube.getZ();
        float noData = sCube.getNoData();   
         
        MMAGrid outputGrid = new MMAGrid(x,y);
        float resultData[][] = new float[y][x];
        float temp[] = new float[z];
        
        for ( int j=0; j <y; j++)
            for ( int i=0; i <x; i++)
            {
                for ( int k=0; k <z; k++)

                    if (sCube.getElement(i,j,k) == noData)
                    {
                        resultData[j][i] = noData;
                        break;
                    }
                    else
                        // Store all the z values in temp
                        temp[k] = sCube.getElement(i,j,k);
                
                if (resultData[j][i] != noData)
                    resultData[j][i] = StatisticalOperator.max(temp,z);
            }
        
        outputGrid.putData(resultData);
        outputGrid.putNoData(noData);
        return outputGrid;
    }
    
   // Roll up a hyper cube along z-dimension
   public MMATimeCube localMultiplyRollUpZ( MMAHyperCube hCube ) throws MMAFunctionInitException
   {
        if (hCube == null)
            throw new MMAFunctionInitException(this.getClass().toString() + " Hyper cube is not initialized.");    

        // Initialze the dimensions of the hypercube and the noData value
        int x = hCube.getX();
        int y = hCube.getY();
        int z = hCube.getZ();
        int t = hCube.getT();
        float noData = hCube.getNoData(); 

        MMATimeCube outputCube = new MMATimeCube(x,y,t);
        float resultData[][][] = new float[t][y][x];
        float temp[] = new float[z];
        
        for ( int l=0; l <t; l++)
            for ( int j=0; j <y; j++)
                for ( int i=0; i <x; i++)
                {
                    for ( int k=0; k <z; k++)

                        if (hCube.getElement(i,j,k,l) == noData)
                        {
                            resultData[l][k][j] = noData;
                            break;
                        }
                        else
                            // Store all the z values in temp
                            temp[k] = hCube.getElement(i,j,k,l);
                
                    if (resultData[l][j][i] != noData)
                        resultData[l][j][i] = StatisticalOperator.max(temp,z);
                }
        
        outputCube.putData(resultData);
        outputCube.putNoData(noData);
        return outputCube;
    }
}
